package com.zang.array;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZVerify
 * @since 2022/10/09 16:06
 * @see <a href="https://leetcode.cn/problems/spiral-matrix/">...</a>
 **/
public class 螺旋矩阵 {

    public List<Integer> spiralOrder(int[][] matrix) {

        ArrayList<Integer> list = new ArrayList<>();
        int top = 0;
        int bottom = matrix.length-1;
        int left = 0;
        int right = matrix[0].length-1;

        while (true){

            for (int i = left;i<=right;i++){
                list.add(matrix[top][i]);
            }
            top++;
            if (top>bottom){
                break;
            }

            for (int i = top;i<=bottom;i++){
                list.add(matrix[right][i]);
            }
            right--;
            if (right<left){
                break;
            }

            for (int i = right;i>=left;i--){
                list.add(matrix[bottom][i]);
            }

            bottom++;

            if (bottom<top){
                break;
            }

            for (int i = bottom;i>=top;i--){
                list.add(matrix[i][left]);
            }
            left++;
            if(left>right){
                break;
            }
        }

        return list;


    }

}
